# coding: utf8
import os

##################
# change language
# languages that don't need translation:
#
T.current_languages = ['en', 'en-en', 'en-ca']

# select user specified _languageuage (via args, session or browser config)
#
if '_language' in request.vars:
    session._language = _language = request.vars._language
elif session._language:
    _language = session._language
elif T.accepted_language is not None:
    _language = T.accepted_language
else:
    _language = "fr-ca"
T.force(_language)

#####################
# plugin information
#
# enable to debug
#plugin_uc_debug_enabled              = True
plugin_uc_debug_enabled              = False

plugin_uc_language_installed         = True
plugin_uc_language_version           = '1.0'
plugin_uc_language_admin_group       = 'plugin_uc_language_admin'
plugin_uc_language_admin_description = 'group to administer plugin_uc_language'


global_plugin_vars_file       = '000_global_plugin_vars' + '_' + plugin_uc_language_version + '_' + '.py'
global_plugin_vars_file_dir   = os.path.join('.','applications',request.application,'models')
global_plugin_vars_file_path  = os.path.join(global_plugin_vars_file_dir, global_plugin_vars_file)
global_vars                   = global_plugin_vars_file_path

def ensure_file(file):
    if not os.path.exists(file):
        open(file,'w').close()

def append_file(file,text):
    myfile = open(file,'a')
    myfile.write(text)
    if 'plugin_uc_debug_enabled == True' in globals():
        print('appended %s to %s' % (text,file))
    myfile.close()

##########################
# add administrative user
#
if not 'plugin_uc_language_admin_created' in globals():
    if not db(db.auth_group.role=='plugin_uc_language_admin').select():
        db.auth_group.insert(role='plugin_uc_language_admin',description = plugin_uc_language_admin_description)
        append_file(global_vars,'plugin_uc_language_admin_created = True\n')
        append_file(global_vars,'plugin_uc_language_version = %s' %(plugin_uc_language_version))
else:
    if 'plugin_uc_debug_enabled' in globals():
        if plugin_uc_debug_enabled == True:
            print('found "plugin_uc_language_admin_created" in globals')

####################################################################
# Ensure we have the global variable 'plugin_uc_language_installed'
#
if not 'plugin_uc_language_installed' in globals():
    append_file(global_vars,'plugin_uc_language_installed = True\n')
else:
    if 'plugin_uc_debug_enabled' in globals():
        if plugin_uc_debug_enabled == True:
            print("found 'plugin_uc_language_installed' in globals")

##################################
# Create our language change menu
#
response.menu_language = []
if T.accepted_language != 'en':
    response.menu_language += [
        ('English', False, URL(r=request,vars=dict(_language='en')),
    )]    
if T.accepted_language != 'fr' and T.accepted_language != 'fr-ca' and T.accepted_language != 'fr-fr':
    response.menu_language += [
        ('Français', False, URL(r=request,vars=dict(_language='fr-ca')),
    )]

response.menu += response.menu_language
